package com.liugui;

/**
 * @ClassName: ThreadDemo2
 * @Author: liugui
 * @Date: 2021-05-19 14:40
 **/
public class ThreadDemo2 {
    public static void main(String[] args) {
        MyThread3 myThread3 = new MyThread3();
        Thread t = new Thread(myThread3);
        t.start();
        //new Thread(myThread3,"测试线程α").start();
        for (int i = 0; i < 10; i++) {
            if(i > 3){
                try {
                    //t 获取执行权 其他线程等待它先执行完
                    t.join();
                    long startTime = System.currentTimeMillis();
                    System.out.println("睡眠开始：" + startTime);
                    //线程睡眠
                    Thread.sleep(500);
                    long endTime =  System.currentTimeMillis();
                    System.out.println("睡眠结束：" + endTime);
                    System.out.println("睡眠时长：" + (endTime - startTime));
                } catch (InterruptedException e) {
                    System.out.println("Thread 强制执行异常");
                }
            }
            System.out.println(Thread.currentThread().getName() + "*****正在运行：" +  i);
        }
    }
}

class MyThread3 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            //获取线程名
            System.out.println(Thread.currentThread().getName() + "正在执行：" + i );
        }
    }
}

